﻿using System;
using System.Data;
using BP.DA;
using BP.En;
using BP.WF;

namespace BP.WF.DTS
{
    /// <summary>
    /// 升级ccflow6 要执行的调度
    /// </summary>
    public class GenerYNJTTrack : Method
    {
        /// <summary>
        /// 不带有参数的方法
        /// </summary>
        public GenerYNJTTrack()
        {
            Title = "生成历史审核数据.";
            Help = "xxxx";
        }
        /// <summary>
        /// 设置执行变量
        /// </summary>
        /// <returns></returns>
        public override void Init()
        {
        }
        /// <summary>
        /// 当前的操纵员是否可以执行这个方法
        /// </summary>
        public override bool IsCanDo
        {
            get
            {
                if (Web.WebUser.No.Equals("admin") == true)
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 执行
        /// </summary>
        /// <returns>返回执行结果</returns>
        public override object Do()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TableName");
            dt.Columns.Add("TableDesc");
            dt.Columns.Add("FrmID");
            dt.Columns.Add("FlowNos");

            DataRow dr = dt.NewRow();
            dr["TableName"] = "pur_contract";
            dr["TableDesc"] = "协作合同";
            dr["FrmID"] = "249";
            dr["FlowNos"] = ",737,735,715,707,676,";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["TableName"] = "C_jgcdjmx";
            dr["TableDesc"] = "甲供材单价明细";
            dr["FrmID"] = "1046";
            dr["FlowNos"] = ",743,739,738,714,713,683,578,";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["TableName"] = "Def_CLNDJH";
            dr["TableDesc"] = "协作单位台账建立表";
            dr["FrmID"] = "1129";
            dr["FlowNos"] = ",736,675,632,";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["TableName"] = "Def_DXLWHZGCJS";
            dr["TableDesc"] = "对下劳务合作收方明细表";
            dr["FrmID"] = "1136";
            dr["FlowNos"] = ",677,638,";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["TableName"] = "F_dxgcjstjb";
            dr["TableDesc"] = "对下劳务合作收方统计表";
            dr["FrmID"] = "1135";
            dr["FlowNos"] = ",682,635,";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["TableName"] = "F_fenbaojiesuan";
            dr["TableDesc"] = "对下计量支付";
            dr["FrmID"] = "250";
            dr["FlowNos"] = ",684,";
            dt.Rows.Add(dr);

            string msg = "";
            foreach (DataRow mydr in dt.Rows)
            {
                string? tableName = mydr[0].ToString();
                string? tableDesc = mydr[1].ToString();
                string? frmID = mydr[2].ToString();
                string? flowNos = mydr[3].ToString();

                msg += "开始处理:" + tableDesc + "," + tableName + "数据.";

               string sql = "SELECT WorkID FROM " + tableName + " WHERE WorkID > 0 ";
                DataTable billDT = DBAccess.RunSQLReturnTable(sql);
                foreach (DataRow billDR in billDT.Rows)
                {
                    string? workID = billDR[0].ToString();

                    //根据workID找它的流程.
                    string flowNo = DBAccess.RunSQLReturnStringIsNull("SELECT FlowNo FROM FlowCurrent WHERE WorkID=" + workID, null);
                    if (flowNo == null)
                    {
                        msg += "err@没有找到流程编号:" + workID;
                        continue;
                    }

                    string ndTrack = "ND" + int.Parse(flowNo) + "Track";

                    sql = "SELECT * FROM " + ndTrack + " WHERE WorkID=" + workID + " ORDER BY RDT";
                    DataTable track = DBAccess.RunSQLReturnTable(sql);

                    string info = "";
                    foreach (DataRow drTrack in track.Rows)
                    {
                        info += "\t\n 节点:" + drTrack["NDFromT"].ToString() + "   处理人:" + drTrack["EmpFromT"].ToString() + "    时间:" + drTrack["RDT"].ToString();
                        info += "\t\n 信息:" + drTrack["Msg"].ToString();
                        info += "<hr/>";
                    }

                    sql = "UPDATE " + tableName + " SET History='" + info + "' WHERE  WorkID=" + workID;
                    DBAccess.RunSQL(sql);
                    msg += " 记录:" + workID + " 增加成功.";
                }
            }
            return "执行信息:" + msg;

        }
    }
}
